package com.topisv.tms.service.work;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.topisv.tms.entity.EdiGoodsSku;
import com.topisv.tms.entity.work.WorkTaskItem;
import com.topisv.tms.repository.EdiGoodsSkuDao;
import com.topisv.tms.repository.work.WorkTaskItemDao;

/**
 * 作业批次_拣货信息明细_service
 * 
 * @author ch
 * 
 */
@Component
@Transactional(readOnly = true)
public class WorkTaskItemService {

	@Autowired
	private WorkTaskItemDao workTaskItemDao;
	@Autowired
	private EdiGoodsSkuDao ediGoodsSkuDao;
	@Autowired
	private WorkTaskService workTaskService;
	
	public WorkTaskItem findOne(Long arg0){
		return workTaskItemDao.findOne(arg0);
	}
	
	
	public List<WorkTaskItem> findUncompletedTaskItemBy(String taskCode){
		return workTaskItemDao.loadUnPickItemByTaskCode(taskCode);
	}
	
	public WorkTaskItem findByTaskCodeAndRankno(String taskCode,int rankno){
		return workTaskItemDao.findByTaskCodeAndRankno(taskCode, rankno);
	}
	
	/**
	 * @param taskCode
	 * @return
	 */
	public List<WorkTaskItem> loadByTaskCode(String taskCode) {
		return this.workTaskItemDao.loadByTaskCode(taskCode);
	}
	
	/**
	 * 查询拣货任务项
	 * @param param
	 * @return
	 */
	public List<WorkTaskItem> loadByParam(Map<String, Object> param) {
		Specification<WorkTaskItem> spec = getWhereClause(param);
		Sort sort = new Sort(Direction.ASC, "id");
		List<WorkTaskItem> result= this.workTaskItemDao.findAll(spec,sort);
		for (WorkTaskItem workTaskItem : result) {
			EdiGoodsSku ediGoodsSku = ediGoodsSkuDao.findByCustomerCodeAndSku(workTaskItem.getCustomerCode(), workTaskItem.getSku());
			if (ediGoodsSku != null) {
				workTaskItem.setSkuName(ediGoodsSku.getSkuName());
			}
		}
		Collections.sort(result,new Comparator<WorkTaskItem>() {
			@Override
			public int compare(WorkTaskItem o1, WorkTaskItem o2) {
				return o1.getSellFlg() == null ? 1 : o2.getSellFlg() == null ? -1 : o2.getSellFlg().compareTo(o1.getSellFlg());
			}
		});
		workTaskService.updatePickInfo(result.get(0).getTaskCode());
		return result;
	}
	
	/**
	 * 查询条件封装
	 * @param param
	 * @return
	 */
	private Specification<WorkTaskItem> getWhereClause(final Map<String, Object> param) {
		
		return new Specification<WorkTaskItem>(){
			
		    @Override
		    public Predicate toPredicate(Root<WorkTaskItem> r, CriteriaQuery<?> q, CriteriaBuilder cb) {
		      Predicate pre = cb.conjunction();
		      
		      try {
				
		      String taskCode=(String) param.get("taskCode");
		      if(StringUtils.isNotBlank(taskCode)){
					pre.getExpressions().add(cb.equal(r.get("taskCode"),taskCode));
			  }

			  String status=(String) param.get("status");
		      if(StringUtils.isNotBlank(status)){
		    		  pre.getExpressions().add(cb.equal(r.get("pickStatus"),status));
			  }
		      
		      } catch (Exception e) {
					
			  }
		      
		      return pre;
		    }
		  };
    }

	/**
	 * 查询
	 * @param orderno
	 * @return
	 */
	public List<WorkTaskItem> loadByOrderno(String orderno) {
		return this.workTaskItemDao.loadByOrderno(orderno);
	}

}